// Logistics/SearchGoods/SearchGoods.js
/*{
单元名称：来货查询页面js
单元功能：选择客户、发货日期查询货物运单信息
所属模块功能：
创建人：yan
创建时间：230831
修改人：
修改时间：
}*/
let logisticsBehavior = require('../behavior/logistics')
Page({
  behaviors: [logisticsBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    orderid: '',
    waybill: {},
    field: [
      {field_id: 'yunshudian',label: '运输点',type: 'picklist',option: [],optionId: 'row_id',optionName: 'mingchen',is_null: false},
      {field_id: 'kehudanwei',label: '客户单位',type: 'reference1',is_null: true,mainEntity: 'customer',showFields: 'customer_name',optionList: [],optionName: 'warehouse_name',optionId: 'row_id'},
      {field_id: 'chukuriqi',label: '发货日期',type: 'datetime'},
    ],

  },
  components: {

  },

  bindChange(e){
    let detail = e.detail,
      field = detail.field,
      id = field.field_id || field.fieldName,
      type = field.type,
      val = detail.val,
      val2 = '',
      obj = this.data.inputData;

    if (type === 'num' || type === 'currency' || type === 'percent'){
      val = val ? val : null
    }

    //赋值
    this.data.inputData[id] = val;

  },

  //搜索
  onSearch(){

    let url1 = 'WLAuth/LaiHuo/YunDan',  //运单接口
      { inputData } = this.data;
    if (!inputData.yunshudian){
      wx.$toast('当前位置无匹配运输点不能查询！');
      return
    }
    let url2 = `WLAuth/LaiHuo/HuoWuHuiZong`;  //货物列表接口

    wx.showLoading({mask: true});
    Promise.all([wx.$get(5,url1,inputData),wx.$get(5,url2,inputData)]).then(res => {
      console.log(res);
      wx.hideLoading();
      let obj = {};
      res.forEach((item,index) => {
        if (item.errcode != 0) return;
        let data = item.data.row || [],
          name = index ? 'goodsList' : 'transitOrder';
        obj[name] = data;
      });
      this.setData(obj);
    })
    /*wx.$get(5,url1,inputData).then(res => {
      if (res.errcode != 0) return;

    })
    wx.$get(5,url2,inputData).then(res => {
      if (res.errcode != 0) return
    })*/
  },


  //  获取运输点
  getTransitPoint(){
    wx.showLoading({mask: true})
    this.wxLocation().then(res => {
      let{latitude, longitude} = res,
        post = {lat:latitude, lng:longitude},
        url = `WLAuth/FuJinYunShuDian`;
      wx.$get(5,url,post).then(res => {
        if (res.errcode != 0) return
        let rows = res.data.row || [],
          obj = {['field[0].option']: rows},
          info = this.data.info;
        this.setData({['field[0].option']: rows});
        if (!rows.length){
          wx.$toast('当前位置无匹配运输点不能查询！');
          return
        }
        //默认填充第一条
        let {row_id,mingchen,weizhi,weidu,jingdu} = rows[0];
        this.setData({
          ['inputData.yunshudian']: row_id
        });
        wx.hideLoading();
        this.onSearch();

      })
    })
  },

  //  获取当前位置
  wxLocation(){
    return new Promise(resolve => {
      // resolve({latitude:23.035508897569443 , longitude:113.11290771484374});
      wx.getLocation({
        type: 'gcj02', // 比较精确
        success: (res) => {
          console.log(res);
          resolve(res);
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {orderid} = options;

    this.getTransitPoint()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
